package com.qf.xiaomi.web.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.xiaomi.service.impl.orderServiceImpl;
import com.qf.xiaomi.service.orderService;
import com.qf.xiaomi.utils.StringConstans;
import com.qf.xiaomi.web.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 田贵宾 on 2022/8/20 16:21
 *
 * @author 田贵宾
 * @version 1.0
 * //TODO java练习阶段
 * @since 1.0
 */
@WebServlet("/result")
public class ResultServlet extends BaseServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    orderService service = new orderServiceImpl();
    public String defaultMethod(HttpServletRequest req,HttpServletResponse resp){
        String resultJson = req.getParameter("result");
        //得到一个Json字符串
        try {
            Map map = objectMapper.readValue(resultJson, Map.class);
            Map result = (Map) map.get("result");
            String oid = (String) result.get("out_trade_no");
            //根据订单号改成已支付
            service.updateStatus(oid,"1");
            return StringConstans.REDIRECT+"/orderList.jsp";
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return  null;
    }
}
